﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Entity.Validation;
using HYI.SDBSERVER;
using eHCBaseClass;
using Sunny.UI;

namespace SHRBASE
{
    public partial class FMS_TS_SUPPLIER : SDBSERVER.FMSINGLE
    {
        //入口函数
        public static Int32 StartMe(string sMainPara, string sPara, string sUser, DBSERVER sDB)
        {
            StaticClass.Selectm_MainPara = sMainPara;
            StaticClass.SelectItemValue = sPara;
            StaticClass.SelectUser = sUser;
            StaticClass.myDB = sDB;
            return 0;
        }

        private string sType = "V";
        private string sSite_No;
        //DBSERVER myADO = new DBSERVER();
        DBCache myCache = new DBCache();
        private int iGridView = 0;

        public FMS_TS_SUPPLIER()
        {
            InitializeComponent();
        }

        //数据保存成功后执行操作
        private void doProc()
        {
            string sql = " update TABLEVERSION set VERSION=VERSION+1 where TABLENAME='S_TS_SUPPLIER'";
            StaticClass.myDB.doexec(sql);

            myCache.doCache();
        }

        //查询代码
        protected override void OnSys04()
        {
            string SQL = "select * from s_ts_supplier where 1=1";
            DataRowView drv = (DataRowView)cbfstate.SelectedItem;
            if ((drv != null) && (cbfstate.SelectedIndex > 0))
            {
                string sfstate = drv.Row["fDictNo"].ToString();
                SQL = SQL + " and fstate='" + sfstate + "'";
            }

            if (qifsupno.Text != "")
            {
                SQL = SQL + " and ((fsupno like '%" + qifsupno.Text + "%')"
                 + " or (fsupname like '%" + qifsupno.Text + "%'))";
            }
            SQL = SQL + "  order by fsupno";
            Qrytable = StaticClass.myDB.query(SQL);

            base.OnSys04();
            rowindex = -1;
            
        }

        private void FMS_TS_SUPPLIER_Load(object sender, EventArgs e)
        {
            _INPUTTABLE = "Import_s_ts_supplier";
            sUser = StaticClass.SelectUser;
            string[] strArray = StaticClass.Selectm_MainPara.Split(new char[] { ';' });
            sMenu = strArray[1];
            sSite_No = strArray[0];

            //按钮控制
            sys07.Visible = false; sys07.Tag = "1";
            sys08.Visible = false; sys08.Tag = "1";
            sys09.Visible = false; sys09.Tag = "1";
            sys11.Visible = false; sys11.Tag = "1";

            //下拉框
            DataTable myTs_Dict = myCache.doGetDict("S_TS_DICT", "fDictType=1000");
            uifstate.DataSource = myTs_Dict;
            uifstate.DisplayMember = "fDictName";
            uifstate.ValueMember = "fDictNo";

            DataRow dr1 = myTs_Dict.NewRow();
            dr1["fDictName"] = "请选择";
            dr1["fDictNo"] = "0";
            myTs_Dict.Rows.InsertAt(dr1, 0);
            cbfstate.DataSource = myTs_Dict;
            cbfstate.DisplayMember = "fDictName";
            cbfstate.ValueMember = "fDictNo";
            cbfstate.SelectedIndex = 0;

            //执行查询代码
            OnSys04();

            //表格列显示统一控制
            if (iGridView == 0)
            {
                myCache.doSetGridVIew(sMenu, sUser, uiDGView);
                iGridView = iGridView + 1;
            }
        }

        //新增代码
        protected override void OnSys01()
        {
            uifsupno.Text = "";
            uifsupname.Text = "";
            uiflinkman.Text = "";
            uifphone.Text = "";
            uiftelno.Text = "";
            uifemail.Text = "";
            uifofficeaddress.Text = "";
            uifstate.SelectedIndex=1;

            sType = "A";
            doLoadData();
            uifsupno.ReadOnly = false;

            base.OnSys01();
        }

        //编辑+浏览载入数据
        private void doLoadData()
        {
            //控制界面输入
            Boolean input = false;
            if ((sType.Equals("A")) || (sType.Equals("B")))
            {
                input = false;
            }
            else
            {
                input = true;
            }
            foreach (Control ctrl in uiPanelData.Controls)
            {
                if (ctrl is UITextBox)
                {
                    UITextBox m = (UITextBox)ctrl;
                    m.ReadOnly = input;
                }

                if (ctrl is UIComboBox)
                {
                    UIComboBox m = (UIComboBox)ctrl;
                    m.ReadOnly = input;
                }
            }

            if (!sType.Equals("A"))
            {
                uifsupno.Text = uiDGView["fsupno", rowindex].Value.ToString();
                uifsupname.Text = uiDGView["fsupname", rowindex].Value.ToString();
                uiflinkman.Text = uiDGView["flinkman", rowindex].Value.ToString();
                uifphone.Text = uiDGView["fphone", rowindex].Value.ToString();
                uiftelno.Text = uiDGView["ftelno", rowindex].Value.ToString();
                uifemail.Text = uiDGView["femail", rowindex].Value.ToString();
                uifofficeaddress.Text = uiDGView["fofficeaddress", rowindex].Value.ToString();
                
                int i = 0;
                string sfstate = uiDGView["fstate", rowindex].Value.ToString();
                foreach (System.Data.DataRowView dr in uifstate.Items)
                {
                    string id = dr["fdictno"].ToString();
                    if (id.Equals(sfstate))
                    {
                        uifstate.SelectedIndex = i;
                        break;
                    }
                    i = i + 1;
                }
            }

        }

        //编辑代码
        protected override void OnSys02()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sType = "B";

            doLoadData();

            uifsupno.ReadOnly = true;

            base.OnSys02();
        }

        //删除代码
        protected override void OnSys03()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }

            if (this.ShowAskDialog("确定删除(" + uiDGView["fsupname", rowindex].Value.ToString() + ")?"))
            {
                string sBillNo = uiDGView["fsupno", rowindex].Value.ToString();
                string SQL = "exec proc_biz_dodelete 's_ts_supplier','','" + sBillNo + "',2,'" + sUser + "'";
                DataTable myTable = StaticClass.myDB.query(SQL);

                string sMsg = myTable.Rows[0][0].ToString();
                if (sMsg.Equals("OK"))
                {
                    this.ShowInfoDialog("删除完毕");
                    myCache.doCache();
                    OnSys04();
                }
                else
                {
                    this.ShowInfoDialog(sMsg);
                }
            }
        }

        //浏览代码
        protected override void OnSys10()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sType = "V";

            doLoadData();
            
            base.OnSys10();
        }

        //数据为空检查
        private bool CheckData()
        {
            return CheckEmpty(uifsupno, "请输入供应商编号")
                   && CheckEmpty(uifsupname, "请输入供应商名称");
        }

        //保存代码
        protected override void OnSys08()
        {
            if (!CheckData())
            {
                return;
            }
            DataRowView drv1 = (DataRowView)uifstate.SelectedItem;
            string sfstate = drv1.Row["fdictno"].ToString();

            if (sType == "A")
            {
                try
                {
                    using (XTBasEntities context = new XTBasEntities())
                    {
                        s_ts_supplier myData = context.s_ts_supplier.Find(uifsupno.Text);
                        if (myData is null)
                        {
                            s_ts_supplier myData2 = new s_ts_supplier();
                            myData2.fsupno = uifsupno.Text;
                            myData2.fsupname = uifsupname.Text;
                            myData2.flinkman = uiflinkman.Text;
                            myData2.fphone = uifphone.Text;
                            myData2.ftelno = uiftelno.Text;
                            myData2.femail = uifemail.Text;
                            myData2.fofficeaddress = uifofficeaddress.Text;
                            myData2.foperator = sUser;
                            myData2.fupddate = DateTime.Now;
                            myData2.fstate = int.Parse(sfstate);
                            myData2.fsite_no = sSite_No;
                            context.s_ts_supplier.Add(myData2);
                            context.SaveChanges();
                            doProc();
                            UIMessageTip.ShowOk("添加成功");

                        }
                        else
                        {
                            //Console.WriteLine("添加成功");
                            UIMessageTip.ShowOk("编号已存在");
                            //Console.WriteLine("已存在");
                            return;
                        }

                    }
                }
                catch (DbEntityValidationException dbEx)
                {
                    Console.WriteLine("错误" + dbEx.Message);
                }
            }

            else if (sType == "B")
            {
                using (XTBasEntities context = new XTBasEntities())
                {
                    //查询
                    s_ts_supplier myts_supplier = context.s_ts_supplier.Find(uifsupno.Text);

                    myts_supplier.fsupname = uifsupname.Text;
                    myts_supplier.flinkman = uiflinkman.Text;
                    myts_supplier.fphone = uifphone.Text;
                    myts_supplier.ftelno = uiftelno.Text;
                    myts_supplier.femail = uifemail.Text;
                    myts_supplier.fofficeaddress = uifofficeaddress.Text;
                    myts_supplier.foperator = sUser;
                    myts_supplier.fupddate = DateTime.Now;
                    myts_supplier.fstate = int.Parse(sfstate); 
                    int result = context.SaveChanges();
                    doProc();
                    UIMessageTip.ShowOk("修改成功");
                }
            }

            //刷新cache
            sType = "V";
            base.OnSys08();

            OnSys04();
        }

        private void cbfstate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }
    }
}
